﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;

namespace auxiliary_proj
{
    public partial class Home : UserControl
    {
        //server connection stuff
        private static MySqlCommand comm;
        private static MySqlConnection conn;

        public Home()
        {
            InitializeComponent();           
            tbUserName.Focus();
            
            //center login and welcome panel
            centerPanels();

            this.tbPassword.KeyPress += new KeyPressEventHandler(tbPassword_KeyPress);
            pLoginDrag.MouseDown += new MouseEventHandler(this.pLogin_MouseDown);
            pLoginDrag.MouseMove += new MouseEventHandler(this.pLogin_MouseMove);
            pWelcomeDrag.MouseDown += new MouseEventHandler(this.pWelcome_MouseDown);
            pWelcomeDrag.MouseMove += new MouseEventHandler(this.pWelcome_MouseMove);

            this.pictureBox1.MouseHover +=new EventHandler(pictureBox1_MouseHover);
            this.pictureBox1.MouseLeave+=new EventHandler(pictureBox1_MouseLeave);
        }

        MemoryStream ms;
        byte[] image_arr;
        Point movePanel;

        //drag login panel on home control in main form
        private void pLogin_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                movePanel = e.Location;
            }
        }

        private void pLogin_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.pLogin.Left += e.X - movePanel.X;
                this.pLogin.Top += e.Y - movePanel.Y;
                //if the above IF is ever False, one of the edges went out of bounds of Home, don't update
            }
        }

        //drag welcome panel on home control in main form
        private void pWelcome_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) movePanel = e.Location;
        }

        private void pWelcome_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.pWelcome.Left += e.X - movePanel.X;
                this.pWelcome.Top += e.Y - movePanel.Y;
            }
        }

        //center panels
        private void centerPanels()
        {
            int screenWidth = this.Width / 2;
            int screenHeight = this.Height / 2;
            int panelWidth = this.pLogin.Width / 2;
            int panelHeight = this.pLogin.Height / 2;
            Point center = new Point(screenWidth - panelWidth, screenHeight - panelHeight);
            this.pLogin.Location = center;
            this.pWelcome.Location = center;
        }


        private void btnLoginSetting_Click(object sender, EventArgs e)
        {
            FormSettings fs = new FormSettings();
            fs.StartPosition = FormStartPosition.CenterScreen;
            fs.Show();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //parse server info
            Program.parseServerInfo();

            Employee.login(this.tbUserName.Text, this.tbPassword.Text);
        }

        public void setVisible(bool b)
        {
            pLogin.Visible = b;
        }

        private void closeLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Employee.logout();
        }

        private void convert_photo()
        {
            if (picUser != null)
            {
                ms = new MemoryStream();
                picUser.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                image_arr = ms.ToArray();
            }
        }

        public void qNewMsgCount()
        {
            conn = new MySqlConnection(Program.getConnection());
            string query = "SELECT COUNT(isViewed) FROM MESSAGING WHERE isViewed='0' AND receive_user_id = '" + Employee.User + "'";
            comm = new MySqlCommand(query, conn);
            try
            {
                conn.Open();
                try
                {
                    MySqlDataReader reader = comm.ExecuteReader();
                    reader.Read();
                    Int32 count = Int32.Parse(reader[0].ToString());
                    if (count > 0)
                    {
                        lbnewMsg.Text = "" + count;
                    }
                    else
                    {
                        lbnewMsg.Hide();
                        ConvertToGrayscale();
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                conn.Close();
                pLogin.Hide();
                pWelcome.Show();
            }

        }

        private void pictureBox1_Click_1(object sender, EventArgs e)
        {
            Singleton.showForm<FormNewMessage>(Application.OpenForms);
        }

        private void ConvertToGrayscale()
        {
            Bitmap img = new Bitmap(pictureBox1.BackgroundImage);
            Bitmap newImg = (Bitmap)img.Clone();

            for (int i = 0; i < img.Width; i++)
            {
                for (int j = 0; j < img.Height; j++)
                {
                    Color p = img.GetPixel(i, j);
                    byte gray = (byte)((p.R + p.G + p.B) / 3);

                    newImg.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
                }
            }

            pictureBox1.BackgroundImage = newImg;
        }

        private void btnUserUpdate_Click(object sender, EventArgs e)
        {
            FormUserUpdate fuu = new FormUserUpdate();
            fuu.StartPosition = FormStartPosition.CenterScreen;
            fuu.Show();
        }

        private void tbPassword_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                Employee.login(tbUserName.Text, tbPassword.Text);
            }
        }

        private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Size = new System.Drawing.Size(48, 50);     

        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Size = new System.Drawing.Size(43, 45);
        }
    }
}
